#!/bin/bash

# Don't do this yourself.. use the ruby script pushchefuser.rb in this directory...
#
# Put this file and your public key file on the host you want ot setup and run it
# As root, run the script with three arguments... public-key-file, userid, usergroup.
# The script will create the user and group,  set up passwordless login using the given keyfile
# and it will set up passwordless sudo on the box for this user.
# These seem to be re-reqs for the user that we run chef-client as.

keyfile=$1
userid=idcuser
groupid=idcgroup

if [ $# -lt 2 ]; then
	echo "Usage:  ${basename} <keyfile> [userid] [usergroup]"
	echo "  creates idcuser or the given user on the host given using the provided public key"
else
	if [ ! -f ${keyfile} ]; then
		echo "First argument does not point to a file"
		echo "Usage:  ${basename} <keyfile> [userid] [usergroup]"
		exit
	fi 
	if [ ${userid} != "" ]; then
		userid=$2
	fi
	if [ ${groupid} != "" ]; then
		groupid=$3	
	fi
	groupadd ${groupid}
	useradd -m -g ${groupid} ${userid}
	mkdir -p /home/${userid}/.ssh
	cat ${keyfile} >>   /home/${userid}/.ssh/authorized_keys
	chown -R ${userid}:${groupid} ~idcuser/.ssh
	echo "${userid}    ALL=(ALL)   NOPASSWD:    ALL" >> /etc/sudoers
fi





